package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import qst.com.bacK.Constant;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.dto.RegisterDTO;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.TestPersonnel;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.TestPersonnelService;
import qst.com.bacK.service.UserAdminService;
import qst.com.bacK.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TestPersonnelService testPersonnelService;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private User user;



    // 注册
    @PostMapping("/reg")
    public String register(RegisterDTO registerDTO, HttpSession session) {
        User user = new User();
        user.setLogin(registerDTO.getLogin());
        user.setName(registerDTO.getName());
        user.setPasswd(registerDTO.getPasswd());
        TestPersonnel testPersonnel = new TestPersonnel().setPhone(registerDTO.getPhone()).setGender(registerDTO.getGender())
                .setTeamId(registerDTO.getTeamId()).setUser(user);
        try {
            if (userService.findUserByLoginCount(user.getLogin()) != null) {
                session.setAttribute("_error_key", "账号已存在");
                return "redirect:/user/showAllTeam";
            } else {
                userService.addTestPersonnel(testPersonnel);
                session.setAttribute("_message_key", "用户信息已保存");
                session.setAttribute(Constant.CURRENT_USER, user);
                session.setAttribute(Constant.CURRENT_TESTPERSONNEL, testPersonnel);
                return "redirect:/login.jsp";
            }
        } catch (ExamException e) {
            session.setAttribute("user", user);
            session.setAttribute("_error_key", e.getMessage());
            return "redirect:/user/create.jsp";
        }
    }





    // 用户管理
    @RequestMapping("/list.action")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> userList = userAdminService.findUsers();
        req.setAttribute("userList", userList);
        WebUtil.forward(req, resp, "/user/list.jsp");
    }

    @RequestMapping("/create.action")
    public void createUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        user.setStatus(1);
        user.setType(1);
        req.setAttribute("user", user);
        WebUtil.forward(req, resp, "/user/create.jsp");
    }

    @RequestMapping("/save.action")
    public void saveUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        req.getParameter("");
        user.setId(RequestUtil.getInt(req, "id"));
        user.setLogin(RequestUtil.getString(req, "login"));
        user.setPasswd(RequestUtil.getString(req, "passwd"));
        user.setName(RequestUtil.getString(req, "name"));
        user.setType(RequestUtil.getInt(req, "type"));
        user.setStatus(RequestUtil.getInt(req, "status"));
        try {
            userAdminService.saveUser(user);
            req.getSession().setAttribute("_message_key", "用户信息已保存");
            String url = "/user/view.action?id=" + user.getId();
            resp.sendRedirect(url);
        } catch (ExamException ex) {
            req.setAttribute("user", user);
            req.getSession().setAttribute("_error_key", ex.getMessage());
            WebUtil.forward(req, resp, "/user/create.jsp");
        }
    }

    @RequestMapping("/view.action")
    public void viewUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = RequestUtil.getInt(req, "id");
        User user = userAdminService.findUserById(id);
        req.setAttribute("user", user);
        WebUtil.forward(req, resp, "/user/view.jsp");
    }

    @RequestMapping("/delete.action")
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = RequestUtil.getInt(req, "id");
        userAdminService.deleteUserById(id);
//        WebUtil.redirect(req, resp, "/user/list.action");
        resp.sendRedirect("/user/list.action");
    }

    @RequestMapping("/edit.action")
    public void edit(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        User user = userAdminService.findUserById(id);
        req.setAttribute("user", user);
        WebUtil.forward(req, resp, "/user/edit.jsp");
    }

    @RequestMapping("/update.action")
    public void updateUser(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = new User();
        user.setId(RequestUtil.getInt(req, "id"));
        user.setLogin(RequestUtil.getString(req, "login"));
        user.setName(RequestUtil.getString(req, "name"));
        user.setType(RequestUtil.getInt(req, "type"));
        user.setStatus(RequestUtil.getInt(req, "status"));
        try {
            userAdminService.updateUser(user);
            req.getSession().setAttribute("_message_key", "用户信息已更新到数据库");
//            WebUtil.redirect(req, resp, "/user/view.action?id=" + user.getId());
            resp.sendRedirect("/user/view.action?id=" + user.getId());
        } catch (ExamException ex) {
            req.setAttribute("user", user);
            req.getSession().setAttribute("_error_key", ex.getMessage());
            WebUtil.forward(req, resp, "/user/edit.jsp");

        }
    }

    @RequestMapping("/password.action")
    public void resetPassword(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        userAdminService.resetPassword(id);
        req.getSession().setAttribute("_message_key", "密码已重置为123456");
        WebUtil.forward(req, resp, "/user/list.action");
    }


}
